package com.mz.ozy.activity.home;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.mz.ozy.R;
import com.mz.ozy.WpkApp;
import com.mz.ozy.activity.MainActivity;
import com.mz.ozy.base.BaseActivity;
import com.mz.ozy.bean.ShogBean;
import com.mz.ozy.bean.ShogDetailBean;
import com.mz.ozy.mvp.presenter.ShogaPresenter;
import com.mz.ozy.mvp.view.ShogaContract;
import com.mz.ozy.utils.AppUtils;
import com.mz.ozy.utils.Utils;
import com.mz.ozy.utils.glideUtils.GlideUtils;

import androidx.annotation.Nullable;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.jzvd.Jzvd;
import cn.jzvd.JzvdStd;
import cn.xuzhijun.proportional.ProportionalFrameLayout;

/**
 * @author lifeStyle on 2020/4/26 0026.
 */
public class ShogDetailActivity extends BaseActivity<ShogaContract.View, ShogaPresenter> implements ShogaContract.View {
    @BindView(R.id.vv_course_train_video)
    JzvdStd mVvCourseTrainVideo;
    @BindView(R.id.tv_live_course_title)
    TextView mTvLiveCourseTitle;
    @BindView(R.id.tv_live_course_extra)
    TextView mTvLiveCourseExtra;
    @BindView(R.id.wv_course_explain)
    WebView mWvCourseExplain;
    @BindView(R.id.titleBar)
    RelativeLayout titleBar;
    @BindView(R.id.tv_time)
    TextView tv_time;
    @BindView(R.id.image_show)
    ImageView imageShow;
    //    @BindView(R.id.vv_idyllic_detail)
//    ProportionalFrameLayout mVideoIdyllic;
    @BindView(R.id.vv_idyllic_detail)
    RelativeLayout mVideoIdyllic;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shog_detail);
        ButterKnife.bind(this);
        initTitle();
        tvCenterTitle.setText("详情");
        mPresenter.getInfoDetailSuccess(this, getIntent().getStringExtra("id"));
    }


    /**
     * 设置沉浸式状态栏时，要把标题栏往下移动状态栏的高度
     */
    public void setTitleBarMargin() {
        int length = AppUtils.getStatusBarHeight(this);
        //注意布局是哪种Layout
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(titleBar.getLayoutParams());
        lp.setMargins(0, length, 0, 0);
        titleBar.setLayoutParams(lp);
    }

    @Override
    public void getInfo(ShogBean o) {

    }

    @Override
    protected void onPause() {
        super.onPause();
        Jzvd.resetAllVideos();
    }

    @Override
    public void onBackPressed() {
        if (Jzvd.backPress()) {
            return;
        }
        Uri uri = getIntent().getData();
        if (uri != null) {
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
            super.onBackPressed();
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public void getInfoDetailSuccess(ShogDetailBean homeMerBean) {
        if (homeMerBean.getSctype() == 1) {
            mVideoIdyllic.setVisibility(View.VISIBLE);
        } else {
            imageShow.setVisibility(View.VISIBLE);
        }
        if (homeMerBean.getSctype() == 1) {
            if (!TextUtils.isEmpty(homeMerBean.getVido())) {
                String proxyUrl = WpkApp.getProxy(this).getProxyUrl(homeMerBean.getVido());
                mVvCourseTrainVideo.setUp(proxyUrl, "");
                Glide.with(this).load(homeMerBean.getImg1()).into(mVvCourseTrainVideo.thumbImageView);
            }
        } else {
            GlideUtils.loadImg(this, imageShow, homeMerBean.getImg1(), R.mipmap.default_main_banner);
        }
        mTvLiveCourseTitle.setText(homeMerBean.getTitle());
        //  mTvLiveCourseExtra.setText(/*map.getLongTime() + "分钟 " +*/ homeMerBean.getIntroduction());
        mWvCourseExplain.loadDataWithBaseURL(null, setHtmlContent(Utils.getNewContent(homeMerBean.getContent())), "text/html", "utf-8", null);
        tv_time.setText(homeMerBean.getCreated());
    }


    public String setHtmlContent(String contents) {
        String head = ("<head>" +
                "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\"> "
                + "<style>img{max-width: 100%; width:auto; height:auto;} body{word-break:break-all;background-color:#ffffff;color:#000000;font-size:12px}</style>" +
                "</head>");
        return "<html>" + head + "<body style=\"word-wrap:break-word;\">" + contents + "</body>" + "</html>";
    }
}

